package com.zdyt.opc.controller;


import com.zdyt.opc.common.ResultEntity;
import com.zdyt.opc.entity.*;
import com.zdyt.opc.service.OpcConnectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/opc/link")
public class OpcConnectController {

    @Autowired
    OpcConnectService opcConnectService;


    // 获取所有 host连接信息
    @GetMapping("/connects")
    public ResultEntity<List<ConnectInfo>> getConnects(){
        List<ConnectInfo> connectInfos = opcConnectService.getConnects();
        return ResultEntity.successWithData(connectInfos);
    }

    // 创建 或 更新 连接信息
    @PostMapping("/createOrUpdate/connect")
    public ResultEntity<List<ConnectInfo>> createOrUpdateConnect(ConnectInfo connectInfo){
        List<ConnectInfo> connectInfos = opcConnectService.createOrUpdateConnect(connectInfo);
        return ResultEntity.successWithData(connectInfos);
    }

    // 删除连接信息
    @DeleteMapping("/delete/connectInfo/{id}")
    public ResultEntity<List<ConnectInfo>> deleteConnectInfo(@PathVariable("id") Integer hostId){
        List<ConnectInfo> connectInfos = opcConnectService.deleteConnectInfo(hostId);
        return ResultEntity.successWithData(connectInfos);
    }

    // 查看当前正在连接的信息
    @GetMapping("/get/connectNow")
    public ResultEntity<NowConnect> getNowConnectInfo(){
        NowConnect nowConnect = opcConnectService.getNowConnectInfo();
        return ResultEntity.successWithData(nowConnect);
    }

    // 连接Host
    @PostMapping("/try/connect")
    public ResultEntity<List<OPCServerDetail>> connectByBaseInfo(ConnectInfo connectInfo){
        List<OPCServerDetail> serverDetails = opcConnectService.connectByBaseInfo(connectInfo);
        return ResultEntity.successWithData(serverDetails);
    }

    // 连接OPCServer
    @GetMapping("/connect/server")
    public ResultEntity<List<Item>> connectToOpcByProgId(String progId){
        List<Item> allItems = opcConnectService.connectToOpcByProgId(progId);
        return ResultEntity.successWithData(allItems);
    }

    // 断开OPCServer连接
    @GetMapping("/disconnect")
    public ResultEntity<String> disconnectOpcServer(){
        opcConnectService.disconnectOpcServer();
        return ResultEntity.successWithoutData();
    }

    // 获取 host下所有的OPCServer
    @GetMapping("/servers")
    public ResultEntity<List<OPCServerDetail>> getAllOPCServerProgIds(){
        List<OPCServerDetail> allOpcServers = opcConnectService.getAllOPCServerProgIds();
        return ResultEntity.successWithData(allOpcServers);
    }


    @Deprecated
    // 获取基本连接信息
    @GetMapping("/connectInfo")
    public ResultEntity<ConnectInfo> getConnectionInformation(){
        ConnectInfo connectInfo = opcConnectService.getConnectionInformation();
        return ResultEntity.successWithData(connectInfo);
    }

    @Deprecated
    // Host是否可以连接
    @GetMapping("/canConnect")
    public ResultEntity<ServerListCanConnect> serverListCanConnect(){
        ServerListCanConnect canConnectHost = opcConnectService.canConnectHost();
        return ResultEntity.successWithData(canConnectHost);
    }

    @Deprecated
    // OPCServer连接状态
    @GetMapping("/isConnect")
    public ResultEntity<OpcServerSate> opcServerIsConnected(){
        OpcServerSate opcServerSate = opcConnectService.isOpcConnected();
        return ResultEntity.successWithData(opcServerSate);
    }

}
